//
//  ParticularsEntryViewController.m
//  bbqpitbooking
//
//  Created by Yin Ni Tun on 5/21/11.
//  Copyright 2011 iss. All rights reserved.
//

#import "ParticularsEntryViewController.h"
#import "ConfirmBookingViewController.h"
#import "ParticularModel.h"
#import "Customer.h"
#import "BBQPitBooking.h"

@implementation ParticularsEntryViewController

@synthesize txtName,txtNRIC,txtEmail,txtContact,booking;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	txtName.delegate = self;
	txtNRIC.delegate = self;
	txtEmail.delegate = self;
	txtContact.delegate = self;
	particularModel = [[ParticularModel alloc]init];
	self.title = @"Enter Particulars";
	
	
	NSLog(@"%@",self.booking.bookingDate);
	
	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification object:app];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [txtName resignFirstResponder];
	[txtNRIC resignFirstResponder];
	[txtEmail resignFirstResponder];
	[txtContact resignFirstResponder];
	return YES;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	[booking release];
	[particularModel release];
}


- (void)dealloc {
    [super dealloc];
}

- (IBAction) Continue
{
	//NSLog(@"%i",[self.booking.selectedPits count]);
	NSString *name = txtName.text;
	NSString *nric = txtNRIC.text;
	NSString *contact = txtContact.text;
	NSString *email = txtEmail.text;
	BOOL result = [particularModel checkParticulars: name andNRIC: nric andContact: contact andEmail :email];
	BOOL emailValidate = [particularModel validateEmail:email];
	BOOL nricValidate = [particularModel validateNRIC:nric];
	if (result) {
		if (emailValidate) {
			if (nricValidate) {
				Customer *testCustomer = [[Customer alloc] init];
				testCustomer.customerName = name;
				testCustomer.customerNRIC = nric;
				testCustomer.phoneNum = contact;
				testCustomer.email=email;
				
				self.booking.customer = testCustomer;
				
				ConfirmBookingViewController *confirmBookingViewController = [[ConfirmBookingViewController alloc]
																			  initWithNibName:@"ConfirmBookingViewController"
																			  bundle:nil];
				confirmBookingViewController.booking = self.booking;
				confirmBookingViewController.booking.bookingID = self.booking.bookingID;
				
				[self.navigationController 
				 pushViewController:confirmBookingViewController
				 animated:YES];
				[confirmBookingViewController release];
			}else {
				UIAlertView *alertPopUp=[[UIAlertView alloc]
										 initWithTitle:@"BBQ Pit Booking" message:@"Please enter the valid NRIC!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
				[alertPopUp show];
				[alertPopUp release];
			}

			
		}else {
			UIAlertView *alertPopUp=[[UIAlertView alloc]
									 initWithTitle:@"BBQ Pit Booking" message:@"Please enter the valid email address!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
			[alertPopUp show];
			[alertPopUp release];
		}

	}else {
		UIAlertView *alertPopUp=[[UIAlertView alloc]
								 initWithTitle:@"BBQ Pit Booking" message:@"Please Enter All the Information" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alertPopUp show];
		[alertPopUp release];
		
	}

}
					

@end
